//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using UnityEditor;
using System.Collections;
using ViNoToolkit;

/// <summary>
/// Dialog item inspector.
/// </summary>
public class DialogItemInspector : EditorWindow {
	public Vector2 m_ScrollPos = Vector2.zero;
	public DialogPartNode current;

	public int m_CurrentID = 0;
	public int m_DialogItemNum = 0;
	public DialogPartData currentData;

//	static private string[] kDummyContents = { "NO ENTRY" }; 
										
	[MenuItem( "Window/ViNo/DialogItemInspector") ]
	static public void Init(){
		GetWindow( typeof( DialogItemInspector) );
//		LoadIcons();		
	}
	
	/// <summary>
	/// Inits the with.
	/// </summary>
	/// <param name="node">Node.</param>
	/// <param name="id">Identifier.</param>
	public void InitWith( DialogPartNode node , int id ){
		current = node;
		m_CurrentID = id;
		UpdateGameView();
	}

	void UpdateGameView(){
		if( current != null ){
			if( current.dlgDataList.Count <= m_CurrentID ){
					m_CurrentID = current.dlgDataList.Count - 1;
			}
			currentData = current.dlgDataList[ m_CurrentID ];//current.GetItemAt( m_CurrentID );
			DialogPartNodeUtility.ViewDialog( currentData , current.m_ViNoTextBox , current.m_NameTextBox );
			DialogPartNodeUtility.UpdateGameView( currentData );
		}
	}
					
	/// <summary>
	/// Raises the GUI event.
	/// </summary>
	void OnGUI(){
		m_ScrollPos = EditorGUILayout.BeginScrollView( m_ScrollPos );

		if( current != null ){
			m_DialogItemNum = current.GetMessageNum();
			GUI.enabled = true;
		}
		else{
			GUI.enabled = false;
		}

		DrawMessageControl();

		if( current != null ){
			if( currentData != null ){
				currentData.show = false;
			}

			if( current.dlgDataList.Count == 0 ){
				ShowNotificate();
			}
			else{
				if( m_CurrentID >= current.dlgDataList.Count ){
					m_CurrentID = 0;
				}
											
				currentData = current.dlgDataList[ m_CurrentID ];//current.GetItemAt( m_CurrentID );
				currentData.show = true;
				
				// --------- Ready to Draw ---------.

//			NodeGUI.DrawDialogItemBar( current , ref currentData , m_CurrentID , current.m_ViNoTextBox , current.m_NameTextBox  );

				switch( currentData.actionID ){
				case DialogPartNodeActionType.Dialog:			
					ViNoEditorGUILayout.DrawEventsCategory( currentData );
					ViNoEditorGUILayout.DrawSoundCategory( currentData );
					ViNoEditorGUILayout.DrawDialogCategory( currentData );
					break;
				case DialogPartNodeActionType.EnterActor:
				case DialogPartNodeActionType.ExitActor:
				case DialogPartNodeActionType.Shake:
				case DialogPartNodeActionType.ChangeState:
				case DialogPartNodeActionType.MoveActor:
					ViNoEditorGUILayout.DrawActorCategory( currentData );
					break;

				case DialogPartNodeActionType.EnterScene:
				case DialogPartNodeActionType.ExitScene:
					ViNoEditorGUILayout.DrawSceneCategory( currentData );
					break;
					
				case DialogPartNodeActionType.GetUserInput:
					if( currentData.inputData == null ){
						currentData.inputData = new GetUserInputData();			
						currentData.inputData.varName = "username";
					}
					currentData.inputData.varName = EditorGUILayout.TextField( "variable name" , currentData.inputData.varName );
					currentData.inputData.rect = EditorGUILayout.RectField( "position" , currentData.inputData.rect );
					break;
				}
			}
		}
		else{
			ShowNotificate();
		}
		
		// If not play mode , Change the scene  and Message .
		if( GUI.changed ){//&& ! Application.isPlaying ){
			UpdateGameView();
		}

		EditorGUILayout.EndScrollView();

		GUI.enabled = true;
	}

	/// <summary>
	/// Shows the notification.
	/// </summary>
	void ShowNotificate(){
		this.ShowNotification( new GUIContent( "Please click Edit button in a DialogPartNode." ));
	}

	void DrawMessageControl(){
//		EditorGUILayout.BeginHorizontal();
			
			m_CurrentID = (int)EditorGUILayout.IntSlider( "Dialog_ID" , m_CurrentID , 0 , m_DialogItemNum - 1 );
//			EditorGUILayout.LabelField(  m_CurrentID.ToString() ,GUILayout.Width( 100f ) );
//		EditorGUILayout.EndHorizontal();

#if false
		EditorGUILayout.BeginHorizontal();

			if( GUILayout.Button( "<<" , GUILayout.Height( 32f ) ) ){
//			if( GUILayout.Button( m_ArrowSeekBackTex , GUILayout.Height( 64f ) ) ){
				m_CurrentID = 0;
			}

			if( GUILayout.Button( "<" , GUILayout.Height( 32f ) ) ){
//			if( GUILayout.Button( m_ArrowBackTex , GUILayout.Height( 64f ) ) ){
				m_CurrentID--;
			}

			if( GUILayout.Button( ">" , GUILayout.Height( 32f ) ) ){
//			if( GUILayout.Button( m_ArrowForwardTex , GUILayout.Height( 64f ) ) ){
				m_CurrentID++;
			}

			if( GUILayout.Button( ">>" , GUILayout.Height( 32f ) ) ){
//			if( GUILayout.Button( m_ArrowSeekForwardTex , GUILayout.Height( 64f ) ) ){
				m_CurrentID = m_DialogItemNum - 1;
			}

			GUI.enabled = true;

			if( m_CurrentID < 0 ){
				m_CurrentID = 0;
			}
			else if( m_CurrentID >= m_DialogItemNum ){
				m_CurrentID = m_DialogItemNum - 1;
			}

		EditorGUILayout.EndHorizontal();
#endif		
	}

}

